//
//  AppDelegate.m
//  17-SandBox
//
//  Created by 曹曹 on 16/8/5.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1.沙盒(SandBox)
    //   <1>Documents(文件文档, 用户主动数据存储)
    //   <2>Library(资源, 一般用来存放程序员要存储的一些数据)
    //       1> Caches(缓存文件)
    //       2> Preference(用户信息, 用户设置, NSUserDefaults)
    //   <3>tmp(临时目录, 下载的临时文件一般放这里)
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 2.获取沙盒路径
    // 快捷获取到对应目录的C函数
    // 根目录
    NSLog(@"Home-----%@", NSHomeDirectory());
    // 临时目录 tmp
    NSLog(@"Temporary-----%@", NSTemporaryDirectory());
    /**
     参数1: 搜索哪个文件夹路径
           常用(1) NSDocumentDirectory
              (2) NSLibraryDirectory
              (3) NSCachesDirectory
     参数2: 在用户作用域下搜索
           常用: NSUserDomainMask
     参数3: YES or NO , YES代表绝对路径(基本上都用这个), NO代表相对路径
     */
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"pathArray-----%@", [pathArray firstObject]);
    // NSBundle   .app文件包
    NSLog(@"%@", [NSBundle mainBundle]);
#pragma mark - 1.简单的文件读写
    // 1.
    NSString *string = @"Hello, I/O File";
    // 一般拼接路径时,使用stringByAppendingPathComponent
    // NSString *writePath = [[pathArray firstObject] stringByAppendingString:@"/hello.txt"];
    NSString *wirtePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.txt"];
    NSError *error = nil;
    [string writeToFile:wirtePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"失败");
    } else {
        NSLog(@"成功");
    }
    // 2.读取对应得到文件
    NSError *readError = nil;
    NSString *readString = [NSString stringWithContentsOfFile:wirtePath encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readString);
    // 3.将数组写入本地文件
    NSArray *array = @[@"含蓄", @"王鍠", @"包铭礼", @"国宝"];
    NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
    BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
    if (isArrayWriteSuccess) {
        NSLog(@"cheng");
    } else {
        NSLog(@"noooo");
    }
    // 4.将数组读取
    NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@", nameArray);
    // 5.将字典写入
    NSDictionary *diction = @{@"name":@"国宝",
                              @"gender":@"male",
                              @"age":@"3333"};
    NSString *dictionPath = [[pathArray firstObject] stringByAppendingPathComponent:@"diction.plist"];
    BOOL isSucess = [diction writeToFile:dictionPath atomically:YES];
    if (isSucess) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
    // 6.字典读取
    NSDictionary *dictionArray = [NSDictionary dictionaryWithContentsOfFile:dictionPath];
    NSLog(@"%@", dictionArray);
    // 7.将NSData写入
    UIImage *image = [UIImage imageNamed:@"welcome_2"];
    // 参数2表示压缩比率
    NSData *data = UIImageJPEGRepresentation(image, 0.1);
    NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData.jpg"];
    UIImagePNGRepresentation(image);
    BOOL isDataSuccess = [data writeToFile:dataPath atomically:YES];
    if (isDataSuccess) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
    // 8.NSData读取
    NSData *imageData = [NSData dataWithContentsOfFile:dataPath];
//    NSLog(@"%@", imageData);
    UIImage *dataImage = [UIImage imageWithData:imageData];
    NSLog(@"%@", dataImage);
#pragma mark - 2.复杂对象文件读写, 自定义类型
    // 归档, 反归档(序列化, 反序列化)
    // 1.归档, 将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookType = @"教育类";
    book.bookPrice = @"998.9";
    book.bookAuthor = @"王鍠";
    book.bookAddress = @"王鍠黑科技出版社";
    UIImage *bookImage = [UIImage imageNamed:@"welcome_2"];
    NSData *bookData = UIImageJPEGRepresentation(bookImage, 0.1);
    book.bookData = bookData;
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
    BOOL isBookSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookSuccess) {
        NSLog(@"yyyyy");
    } else {
        NSLog(@"nnnnn");
    }
    // 2.反归档
    Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@", huangBook.bookName);
    UIImage *newImage = [UIImage imageWithData:huangBook.bookData];
    NSLog(@"000000%@", newImage);
    
    // 如果对象想要实现归档和反归档
    // 1.对象对应的类需要签订<NSCoding>
    // 2.实现协议方法
    //   1> initWithCoder反归档用
    //   2> encodeWithCoder归档用
    // 3.归档时使用NSKeyedArchiver
    // 4.反归档使用NSKeyedUnArchiver
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
